package com.lf.sp.controller.portal;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lf.sp.common.Const;
import com.lf.sp.common.ResponseCode;
import com.lf.sp.common.ServerResponse;
import com.lf.sp.pojo.User;
import com.lf.sp.service.ICartService;
import com.lf.sp.vo.CartVo;

@Controller
@RequestMapping("/cart/")
public class CartController {
	
	@Autowired
	private ICartService iCartService;
	/**
	 * 商品添加到购物车中
	 * @param session
	 * @param count
	 * @param productId
	 * @return
	 */
	@RequestMapping(value="add.do")
	@ResponseBody
	public ServerResponse<CartVo> add(HttpSession session , Integer count ,Integer productId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return  iCartService.add(user.getId(), productId, count) ;
	}
	
	/**
	 * 更新购物车中商品
	 * @param session
	 * @param count
	 * @param productId
	 * @return
	 */
	@RequestMapping(value="update.do")
	@ResponseBody
	public ServerResponse update(HttpSession session,Integer count,Integer productId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return iCartService.update(user.getId(), productId, count);
	}
	
	/**
	 * 删除购物车中商品
	 * @param session
	 * @param productIds
	 * @return
	 */
	@RequestMapping(value="delete_product.do")
	@ResponseBody
	public ServerResponse deleteProduct(HttpSession session ,String productIds){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return iCartService.deleteProduct(user.getId(), productIds);
		
	}
	/**
	 * 查询购物车商品
	 * @param session
	 * @return
	 */
	@RequestMapping(value="list.do")
	@ResponseBody
	public ServerResponse list(HttpSession session){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return iCartService.list(user.getId());		
	}
	/**
	 * 全选
	 * @param session
	 * @return
	 */
	@RequestMapping(value="select_all.do")
	@ResponseBody
	public ServerResponse selectAll(HttpSession session){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return iCartService.selectOrUnSelect(null,user.getId(), Const.Cart.CHECKED);		
	}
	
	/**
	 * 全反选
	 * @param session
	 * @return
	 */
	@RequestMapping(value="un_select_all.do")
	@ResponseBody
	public ServerResponse unSelectAll(HttpSession session){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return iCartService.selectOrUnSelect(null,user.getId(), Const.Cart.UN_CHECKED);		
	}
	/**
	 * 单选
	 * @param session
	 * @param productId
	 * @return
	 */
	@RequestMapping(value="select.do")
	@ResponseBody
	public ServerResponse select(HttpSession session,Integer productId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return iCartService.selectOrUnSelect(productId,user.getId(), Const.Cart.CHECKED);		
	}
	/**
	 * 单反选
	 * @param session
	 * @param productId
	 * @return
	 */
	@RequestMapping(value="un_select.do")
	@ResponseBody
	public ServerResponse unSelect(HttpSession session,Integer productId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		return iCartService.selectOrUnSelect(productId,user.getId(), Const.Cart.UN_CHECKED);		
	}
	/**
	 * 查询购物车中总的产品数量
	 * @param session
	 * @return
	 */
	@RequestMapping(value="get_cart_product_count.do")
	@ResponseBody
	public ServerResponse<Integer> getCartProductCount(HttpSession session){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user ==null){
			return ServerResponse.createBySuccess(0);
		}
		return iCartService.getCartProductCount(user.getId());		
	}
}
